package com.godtree.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.godtree.entity.User;
import com.godtree.entity.UserInfo;
import com.godtree.persistence.UserDao;
import com.godtree.persistence.UserInfoDao;
import com.google.gson.Gson;

@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_UNCOMMITTED)
/*
 * ISOLATION_READ_UNCOMMITTED:这是事务最低的隔离级别，它允许另外一个事务可以看到这个事务未提交的数据。
 * PROPAGATION_REQUIRED:如果存在一个事务，则支持当前事务。如果没有事务则开启。
 */
public class UserService {
  @Autowired
	private UserDao userDao;
  @Autowired
  private UserInfoDao userInfoDao;
	Gson gson=new Gson();
  public void addInfo(UserInfo userInfo){
	  userInfoDao.makePersistent(userInfo);
   }
	public void addUser(User user){
		userDao.makePersistent(user);
	}
	public void add(User user,UserInfo userInfo){
		userInfoDao.makePersistent(userInfo);
		user.setInfo_id(userInfo.getId());
		userDao.makePersistent(user);
	}
	public void deleteUser(String id){
		User user=userDao.findById(id,false);
		UserInfo userInfo=userInfoDao.findById(user.getInfo_id(), false);
		userDao.makeTransient(user);
		userInfoDao.makeTransient(userInfo);
	}
	public User showUser(String id){
		User user=userDao.findById(id,true);
		UserInfo userInfo=userInfoDao.findById(user.getInfo_id(),true);
    System.out.println(gson.toJson(userInfo));
    System.out.println(gson.toJson(user));
		return user;
	}
	public String list(){
		Gson gson=new Gson();
		System.out.println(gson.toJson(userDao.findAll()));
		return gson.toJson(userDao.findAll());
	}
}
